﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;

namespace HYRElectric.Equipment
{
	public class CenterRoomLayout : EquipmentLayout
	{
		public CenterRoomLayout(UIDocument uiDoc, Document document, DbFamilyObject familyObject, FamilySymbol selectedFamilySymbol, List<BuiltInCategory> lstFilter) : base(uiDoc, document, familyObject, selectedFamilySymbol, lstFilter)
		{
			this.m_selection = uiDoc.Selection;
		}

		public override bool Layout()
		{
			if (!base.Layout())
			{
				return false;
			}
			for (;;)
			{
				List<XYZ> list = new List<XYZ>();
				try
				{
					bool flag = this.IsLinkRevitArchDoc(this.doc);
                    ObjectType objectType = flag ? (ObjectType)5 : (ObjectType)1;
					foreach (Reference reference in this.m_selection.PickObjects(objectType, new RoomSelectionFilter(flag), "请选择房间"))
					{
						Element element = this.doc.GetElement(reference);
						if (element != null)
						{
							if (flag)
							{
								element = (element as RevitLinkInstance).GetLinkDocument().GetElement(reference.LinkedElementId);
							}
							Room room = element as Room;
							if (room != null)
							{
								XYZ xyz = YJKRevitTools.Centroid(room);
								if (xyz == null)
								{
									xyz = (room.Location as LocationPoint).Point;
								}
								if (xyz != null)
								{
									list.Add(xyz);
								}
							}
						}
					}
				}
				catch (Exception)
				{
					break;
				}
				if (list.Count != 0)
				{
					double angle = 0.0;
					TransactionGroup transactionGroup = new TransactionGroup(this.doc, "TransactionGroup");
					try
					{
						transactionGroup.Start();
						Transaction transaction = null;
						try
						{
							foreach (XYZ xyz2 in list)
							{
								transaction = new Transaction(this.doc, "RectLayoutEx");
								transaction.Start();
								FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(this.doc, xyz2, this.familySymbol, this.doc.ActiveView.GenLevel, angle);
								if (familyInstance == null)
								{
									throw new Exception("布置设备出错。");
								}
								base.SetParametersValue(familyInstance, this.dbFamilyObject.Size);
								transaction.Commit();
							}
						}
						catch (Exception)
						{
							if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
							{
								transaction.RollBack();
							}
							break;
						}
						transactionGroup.Assimilate();
						continue;
					}
					catch (Exception)
					{
						transactionGroup.RollBack();
						continue;
					}
					break;
				}
			}
			return true;
		}

		public bool IsLinkRevitArchDoc(Document doc)
		{
			return new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms).ToElementIds().Count == 0;
		}

		private Selection m_selection;
	}
}
